package 中国.珍林.AI.图片.分类;

import com.fasterxml.jackson.core.type.TypeReference;
import lombok.extern.log4j.Log4j2;
import 中国.易.工具.Http工具;
import 中国.易.工具.Json工具;

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

@Log4j2
public class Clip客户端
{
    HttpClient httpClient = HttpClient.newHttpClient();

    public double[] 文本转向量(String s)
    {
        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create(配置.clip服务地址 + "/text?i=" + Http工具.路径encode(s))).GET().build();
        try
        {
            HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
            int 返回码 = response.statusCode();
            String body = response.body();
            if (返回码 != 200)
            {
                log.error("文本转向量 服务报404错误,{}", body);
                throw new LLM服务调用异常("404");
            }
            log.info("文本转向量 最终返回长度:{}", body.length());
            double[][] ds = Json工具.字符串转对象(body, new TypeReference<>()
            {
            });
            return ds[0];
        }
        catch (Exception e)
        {
            throw new RuntimeException(e);
        }
    }

    public double[] 图片转向量(byte[] image, String 文件名)
    {
        String type = "image/jpeg";
        if (文件名.toLowerCase().endsWith("png"))
        {
            type = "image/png";
        }
        else if (文件名.toLowerCase().endsWith("webp"))
        {
            type = "image/webp";
        }
        // 构建 multipart/form-data 请求体
        String boundary = "Boundary-" + System.currentTimeMillis();
        String multipartFormData1 =
                "--" + boundary + "\r\n" + "Content-Disposition: form-data; name=\"file\"; filename=\"" + 文件名
                + "\"\r\n" + "Content-Type:" + type + " \r\n\r\n";

        String multipartFormData2 = "\r\n" + "--" + boundary + "--\r\n";

        byte[] formData = mergeArrays(multipartFormData1.getBytes(), image, multipartFormData2.getBytes());
        String contentType = "multipart/form-data; boundary=" + boundary;

        HttpRequest request = HttpRequest.newBuilder().uri(URI.create(配置.clip服务地址 + "/f"))
                .header("Content-Type", contentType).POST(HttpRequest.BodyPublishers.ofByteArray(formData)).build();

        try
        {
            HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
            int 返回码 = response.statusCode();
            String body = response.body();
            if (返回码 != 200)
            {
                log.error("图片转向量 服务报404错误,{}", body);
                throw new LLM服务调用异常("404");
            }
            log.info("图片转向量 最终返回长度:{}", body.length());
            double[][] ds = Json工具.字符串转对象(body, new TypeReference<>()
            {
            });
            return ds[0];
        }
        catch (Exception e)
        {
            throw new RuntimeException(e);
        }
    }

    public static byte[] mergeArrays(byte[]... arrays)
    {
        int totalLength = 0;
        for (byte[] array : arrays)
        {
            totalLength += array.length;
        }
        byte[] result = new byte[totalLength];
        int currentIndex = 0;
        for (byte[] array : arrays)
        {
            System.arraycopy(array, 0, result, currentIndex, array.length);
            currentIndex += array.length;
        }
        return result;
    }
}
